<?php
	require("../wp-blog-header.php");
	require("event-config.php");

	if(get_user_role() != "administrator")
	{
		header("Location: error.php");
	}


	if(!isset($_GET['p']))
    {
        header("Location: error.php");
    }
    else
    {
    	$event_id = $_GET['p'];
    	$query = "SELECT `name`, 
    	DATE_FORMAT(`start_date`,'%Y-%m-%dT%H:%i') AS `start_date`, 
    	DATE_FORMAT(`end_date`,'%Y-%m-%dT%H:%i') AS `end_date`,
    	`benefit`, `description`, `slot` 
    	FROM $EVENT WHERE `event_id` = '$event_id'";

    	$result = first_row($query);
    	if($result){
    		$name = 	   $result['name'];
			$start_date =   $result["start_date"];
			$end_date =     $result["end_date"];
			$benefit =     $result["benefit"];
			$description = $result["description"];
			$slot = 	   $result["slot"];
    	}
	    else{
	    	header("Location: error.php");
	    }
    }
?>

<html>
<head>
	<title>Chỉnh sửa event <?php echo $name; ?></title>
	<meta charset='utf-8' />
    <script type="text/javascript" src="js/jquery.js"></script>
	<script src="ckeditor/ckeditor.js"></script>
	<script src="ckeditor/adapters/jquery.js"></script>
    <link rel="stylesheet" type="text/css" href="css/style.css" />
	<script>
		CKEDITOR.disableAutoInline = true;

		$( document ).ready( function() {
			$( '#description' ).ckeditor();
		} );
	</script>
</head>
<body onload="load()">
	<header>
        <div class="header-wrapper">
            <img src="img/logo.png" class="header-logo" />
            <p class="header-title"><a class="header-link" href="index.php">Quản lí sự kiện</a></p>
            <p class="header-title"><a class="header-link" href="create-event.php">Tạo sự kiện</a></p>
        </div>
    </header>
	<div class='container'>
    	<div class='post-wrapper'>
			<h4>Chỉnh sửa sự kiện</h4>
    		<form class='form'>
				<table>
					<tr>
						<th><label class="form-label">Tên sự kiện</label></th>
						<th colspan="3"><input type="text" name="name" id="name" placeholder="Tên sự kiện" /></th>
					</tr>
					<tr>
						<th><label class="form-label">Ngày bắt đầu:</label></th>
						<th><input type="datetime-local" name="startDate" id="startDate" /></th>
						<th><label class="form-label">Ngày kết thúc:</label></th>
						<th><input type="datetime-local" name="endDate" id="endDate" /></th>
					</tr>
					<tr>
						<th><label class="form-label">Quyền lợi</label></th>
						<th><input type="text" name="benefit" id="benefit" /></th>
						<th><label class="form-label">Số lượng: </label></th>
						<th><input type="number" name="slot" id="slot" min='0' /></th>
					</tr>
					<tr>
						<th colspan="4"><textarea class="ckeditor" cols="80" id="description" name="description" rows="10"></textarea></th>
					</tr>
				</table>
    		</form>
			<button class='submit' type="button" id="btn" onclick="submit(<?php echo $event_id; ?>)">Cập nhật</button>
    		<img class='loading' src='img/loading.gif' />
    	</div>
	</div>

	<script type="text/javascript">
		function load() {
			$('#name').val('<?php echo $name; ?>');
			$('#startDate').val('<?php echo $start_date; ?>');
			$('#endDate').val('<?php echo $end_date; ?>');
			$('#benefit').val('<?php echo $benefit; ?>');
			$('#description').val('<?php echo $description; ?>');
			$('#slot').val('<?php echo $slot; ?>');
		}

		function submit (id) {
			var name = $('#name').val();
			
			if(name == "")
			{
				alert("Cần phải điền tên sự kiện");
				return;
			}
			$("#btn").css("width", "-=16");
			$(".loading").css("display", "initial");

			var startDate = $('#startDate').val();
			var endDate = $('#endDate').val();
			var benefit = $('#benefit').val();
			var description = $('#description').val();
			var slot = $('#slot').val();

			$.post("update-event.php", {id: id, name: name, startDate: startDate, 
				endDate: endDate, benefit: benefit, 
				description: description, slot: slot}).done(function(data){
				if(data == 'fail'){
					alert("Can't update event. Try again!!!");
				}
				else {
					window.location.href = 'event.php?p=' + id;
				}
			});
		}
	</script>
</body>
</html>